#include "qmenuactionmap.h"

QMenuActionMap::~QMenuActionMap()
{
    while(m_action_groups.size()>0)
    {
        qDeleteAll(m_action_groups.first()->m_actions);
        delete m_action_groups.takeFirst();
    }
}

void QMenuActionMap::insert_action(const QString &group, tagActionInfo *action, int index)
{
    tagActionGroupInfo* g=m_id_to_group.value(group);
    if(g!=NULL)
    {
        if(index<0 || index>g->m_actions.size())
        {
            index=g->m_actions.size();
        }
        g->m_actions.insert(index,action);
        m_id_to_action.insert(action->m_id,action);
    }
}

void QMenuActionMap::insert_group(const QString &id, const QString &text, int index)
{
    tagActionGroupInfo *g=m_id_to_group.value(id);
    if(g==NULL)
    {
        g=new tagActionGroupInfo;
        g->m_id=id;
        g->m_text=text;
        g->m_index=index;
        m_id_to_group.insert(id,g);
        for(int i=0;i<m_action_groups.size();i++)
        {
            if(m_action_groups.at(i)->m_index>index)
            {
                m_action_groups.insert(i,g);
                return;
            }
        }
        m_action_groups.append(g);
    }
}

int QMenuActionMap::group_count()
{
    return m_action_groups.size();
}

tagActionGroupInfo* QMenuActionMap::get_group(int index)
{
    if(index<0 || index>= m_action_groups.size())
    {
        return NULL;
    }
    return m_action_groups.at(index);
}

tagActionInfo* QMenuActionMap::get_action(const QString &id)
{
    return m_id_to_action.value(id);
}
